package candies;

import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		try {
			// N - number of children
			// pr - previous child rank
			// pc - previous child candies
			// tc - total candies
			// dsc - decreasing sequence count
			int N = in.nextInt(), pr = 0, pc = 0, tc = 0, dsc = 0;
			boolean ds = false; // is decreasing sequence
			for(int n = 0; n < N; n++) {
				int r = in.nextInt(), c = 0;
				if(r > pr) {
					if(ds) {
						if(pc >= 1)
							tc -= (pc - 1) * dsc;
						else
							tc += (1 - pc) * (dsc + 1);
						ds = false;
						c = 2;
					} else
						c = Math.max(pc + 1, 1);
				}
				else if(r < pr) {
					c = pc - 1;
					if(!ds) {
						ds = true;
						dsc = 1;
					} else
						dsc++;
				}
				else {
					if(ds) {
						if(pc >= 1)
							tc -= (pc - 1) * dsc;
						else
							tc += (1 - pc) * (dsc + 1);
						ds = false;
					}
					c = 1;
				}
				tc += c;
				pr = r;
				pc = c;
			}
			if(ds) {
				if(pc >= 1)
					tc -= (pc - 1) * dsc;
				else
					tc += (1 - pc) * (dsc + 1);
				ds = false;
			}
			System.out.println(tc);
		} finally {
			in.close();
		}
	}
}
